package com.cyzy.service.impl;

import com.cyzy.mapper.ArtGoodMapper;
import com.cyzy.pojo.ArtGood;
import com.cyzy.service.ArtGoodService;
import com.cyzy.vo.ArtSpecResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ArtGoodServiceImpl implements ArtGoodService {
    @Autowired
    private ArtGoodMapper artGoodMapper;
    @Override
    public List<Map<String, Object>> getAllGood(Integer campusId) {
        List<ArtSpecResultVo> allGood = artGoodMapper.getAllGood(campusId);
        // 使用 LinkedHashMap 保持顺序
        Map<String, List<Map<String, Object>>> categoryMap = new LinkedHashMap<>();
        for (ArtSpecResultVo dish : allGood) {
            String categoryName = dish.getGsortName();//类型名，左边栏的信息

            Map<String, Object> food = new HashMap<>();
            food.put("id", dish.getGoodId());//商品Id
            food.put("name", dish.getGoodName());//商品名字
            food.put("stock", dish.getGoodStock());//商品库存
            food.put("desc", dish.getGoodDesc());//商品描述
            food.put("orderPrice", dish.getGoodOriginalPrice());//原价
            food.put("price", dish.getGoodMemberPrice());//现价
            food.put("image", dish.getGoodPicture() != null ? dish.getGoodPicture() : null);//图片
            food.put("count", 0);  // 默认点餐数量为 0

            // 将 food 放入对应分类
            categoryMap.computeIfAbsent(categoryName, k -> new ArrayList<>()).add(food);
        }
        // 构建最终结果格式
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map.Entry<String, List<Map<String, Object>>> entry : categoryMap.entrySet()) {
            Map<String, Object> category = new HashMap<>();
            category.put("name", entry.getKey());
            category.put("foods", entry.getValue());
            result.add(category);
        }
        return result;
    }

    @Override
    public ArtGood getGoodByGoodId(Integer id) {
        return  artGoodMapper.getGoodByGoodId(id);
    }
}
